//
//  CateDetailViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "CateDetailViewController.h"

@interface CateDetailViewController ()

@end

@implementation CateDetailViewController

- (void)dealloc
{
    [_model release];
    [_dataArr release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200*ADJUSTWIDTH, 30*ADJUSTWIDTH)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = self.model.songlist_name;
    self.navigationItem.titleView = self.TitleView;    


    
    
    self.dataArr = [[NSMutableArray alloc] init];
    url_arr = [[NSMutableArray alloc] init];
    
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;
    _normalView.tableView.backgroundColor = [UIColor clearColor];
    [_normalView getURLForImage:self.model.pic_url_240_200 textForLabel:self.model.desc];
    [self.view addSubview:_normalView];
    [_normalView release];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    _normalView.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getDataForCateDetail:1];
    self.nextPage = 2;
    [self addFooter];
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    

    
    // Do any additional setup after loading the view.
}


#pragma mark --上拉加载更多
- (void)addFooter
{
    
    __block  CateDetailViewController *singerVc = self;
    
    [_normalView.tableView addFooterWithCallback:^{
        
        NSLog(@"vc.Nextpage =====  %ld", singerVc.nextPage);
        
        [self getDataForCateDetail:self.nextPage];
        
        self.nextPage++;
    }];
}


- (void)getDataForCateDetail:(NSInteger)page
{
    
    
    NSString *url_one = @"http://api.dongting.com/channel/channel/";
    
    NSString *url_string = [NSString stringWithFormat:@"%@%@/songs?size=50&page=%ld", url_one, self.model.songlist_id, page];
    
    NSLog(@"***** url_string = =%@", url_string);
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        
        NSArray *ArrData = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in ArrData) {
            
            ReDetailModel *model = [[ReDetailModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataArr addObject:model];
            
            [model release];
        }
        
        HUD.hidden = YES;
        _normalView.tableView.separatorColor = [UIColor lightGrayColor];
        [_normalView.tableView reloadData];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
      [_normalView.tableView footerEndRefreshing];
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    ReDetailModel *model = [self.dataArr objectAtIndex:indexPath.row];
    
    [cell getDataForCell:model];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1 ];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return SCREEN_WIDTH*0.213333;
}

#pragma mark --  播放的逻辑
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    for (ReDetailModel *reDetailModel in self.dataArr) {
        
        
        if (reDetailModel.auditionList.count > 1) {
            NSDictionary *dic = [reDetailModel.auditionList objectAtIndex:1];
            NSString *url_string = [dic objectForKey:@"url"];
            [url_arr addObject:url_string];
        } else {
            
            NSDictionary *dic = [reDetailModel.auditionList objectAtIndex:0];
            NSString *url_string = [dic objectForKey:@"url"];
            [url_arr addObject:url_string];
        }
        
   
    }
    
    
    ReDetailModel *reDetailmodel = [self.dataArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = self.dataArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:url_arr indexPath:indexPath];
    
    
    
}
// 播放方法
- (void)playeMusicWithModel:(NSMutableArray *)url_Arr indexPath:(NSIndexPath *)indexpath;
{
    
    
    NSString *url_string = [url_arr objectAtIndex:indexpath.row];
    
    STKAudioPlayer *Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    [Legacy play:url_string];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
